import {
  listColumns,
} from './index.const';
import {
  fileDownload
} from '@const/utils.js';
import {
  $loadFileSimply
} from '@const/loadFileOps.js';
import {
  selectAgentManager,
  selectOriginalFactoryOrder,
  selectsaleTargetOrder,
  downloadNewsPaperData,
  updateRed,
  downloadAllNewsPaperData
} from '@api/m_mall_web_v1.js';
import ElEditable from '@components/el-editable';

export default {
  components: {
    ElEditable,
  },
  data() {
    return {
      height_collapse: false,
      collapse: true,
      // 多条件查询订单
      tableQuery: {
        orderStatus: '',
        originalFactoryCode: '',
        orderCode: '',
        saleTargetCode: '',
        createEnd: '',
        createStart: '',
        saleEnd: '',
        saleStart: '',
        busModel: ''
      },
      saleDate: ['', ''],
      createDate: ['', ''],
      tableLoading: false,
      listColumns: listColumns, //表头
      delDialogVisible: false,
      //列表数据
      payload: {},
      loading: false,
      //销售对象下拉
      saleTargetCodeOptions: [],
      //生产厂家下拉
      originalFactoryCodeOptions: [],
      // 主列表分页
      currentPage: 1,
      pageSize: 20,
      multipleSelection: [],
      redLoding: false
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    if (this.$refs.queryForm.$el.clientHeight > 90) {
      this.height_collapse = true
      this.collapse = false
    }
  },
  methods: {

    // 重置
    queryFormReset() {
      this.tableQuery = {
        orderStatus: '',
        originalFactoryCode: '',
        orderCode: '',
        saleTargetCode: '',
        createEnd: '',
        createStart: '',
        saleEnd: '',
        saleStart: '',
        busModel: ''
      };
      this.saleDate = ['', '']
      this.createDate = ['', '']
      this.currentPage = 1;
      this.getList();
    },
    // 查询
    queryFormQuery() {
      this.currentPage = 1;
      this.getList();
    },
    //销售对象下拉
    saleTargetCodeRemoteMethod(val) {
      if (val !== '') {
        this.loading = true;
        let query = {
          saleTargetName: val,
        };
        this.$axios.$post(selectsaleTargetOrder, query).then(res => {
          this.saleTargetCodeOptions = res.payload;
          this.loading = false;
        });
      }
    },
    // 生产厂家
    originalFactoryCodeRemoteMethod(val) {
      if (val !== '') {
        this.loading = true;
        let query = {
          originalFactoryName: val,
          page: 1,
          pageSize: 999
        };
        this.$axios.$post(selectOriginalFactoryOrder, query).then(res => {
          this.originalFactoryCodeOptions = res.payload;
          this.loading = false;
        });
      }
    },
    // 主列表数据
    getList() {
      this.tableLoading = true;
      const query = {
        ...this.tableQuery,
        page: this.currentPage,
        pageSize: this.pageSize,
        createStart: this.createDate ? this.createDate[0] : '',
        createEnd: this.createDate ? this.createDate[1] : '',
        saleStart: this.saleDate ? this.saleDate[0] : '',
        saleEnd: this.saleDate ? this.saleDate[1] : '',
      };
      this.$axios.$post(selectAgentManager, query).then(res => {
        if (res.code === '0') {
          this.payload = res.payload;
        }
        this.tableLoading = false;
      });
    },
    // 主列表分页器
    reloadProductData(val) {
      this.currentPage = val.number
      this.pageSize = val.size
      this.getList();
    },
    // 导出
    exportEx() {
      if (this.multipleSelection.length > 0) {
        let dataArr = []
        this.multipleSelection.forEach(item => {
          dataArr.push(item.orderId)
        })
        $loadFileSimply({
            method: 'post',
            url: downloadNewsPaperData,
            timeout: '0',
            data: {
              idList: dataArr
            }
          }).then(res => {
            if (res.byteLength) {
              fileDownload(res, '报台明细.xlsx')
            } else {
              this.$message({
                message: '导出失败',
                type: 'warning'
              })
            }
          })
          .catch(err => {
            this.$message({
              message: '导出失败',
              type: 'warning'
            })
          })
      } else {
        this.$message({
          message: '请选择导出数据',
          type: 'warning'
        })
      }

    },
    //全部导出
    exportExAll() {
      if (this.payload.content && this.payload.content.length > 0) {
        $loadFileSimply({
            method: 'post',
            url: downloadAllNewsPaperData,
            timeout: '0',
            data: {
              ...this.tableQuery,
              createStart: this.createDate ? this.createDate[0] : '',
              createEnd: this.createDate ? this.createDate[1] : '',
              saleStart: this.saleDate ? this.saleDate[0] : '',
              saleEnd: this.saleDate ? this.saleDate[1] : '',
            }
          }).then(res => {
            if (res.byteLength) {
              fileDownload(res, '报台明细.xlsx')
            } else {
              this.$message({
                message: '导出失败',
                type: 'warning'
              })
            }
          })
          .catch(err => {
            this.$message({
              message: '导出失败',
              type: 'warning'
            })
          })
      } else {
        this.$message({
          message: '暂无可导出数据',
          type: 'warning'
        })
      }
    },
    //表格选择数据
    selectionChange(val) {
      this.multipleSelection = val
    },
    // 新建报台
    addGo() {
      this.$router.push({
        path: '/report-bills-management/distributor/add'
      });
    },
    // /操作栏-冲红弹窗
    rushRedDialog(scope) {
      this.delDialogVisible = true;
      this.orderIds = scope;
    },
    // 冲红弹窗确认
    rushRedOk() {
      this.redLoding = true
      this.$axios.$post(updateRed, {
        orderId: this.orderIds.orderId
      }).then(res => {
        this.redLoding = false
        if (res.payload && res.payload === 'success') {
          this.$message.success('冲红成功');
          this.getList();
        } else {
          this.$message.error('冲红失败');
        }
      }).catch(err => {
        this.redLoding = false
        console.log(err)
      });
      this.delDialogVisible = false;
    },
    // /操作栏-编辑
    editGo(scope) {
      this.$router.push({
        path: '/report-bills-management/distributor/edit',
        query: {
          id: scope.orderId,
        }
      });
    },
    // /操作栏-查看
    checkDetail(scope) {
      this.$router.push({
        path: '/report-bills-management/distributor/detail',
        query: {
          id: scope.orderId,
        }
      });
    }
  }
};
